<?php

namespace Modules\Admin\Http\Controllers\SystemError;

use Modules\Common\Models\SystemError;
use Modules\Common\Util\Api;


class Retrieve
{


    /**
     * @Route(path=/api/admin/system-error)
     */
    public function retrieve()
    {
        $query = SystemError::latest();

        Api::validate(['type' => 'integer', 'source' => 'integer', 'status' => 'integer', 'reason_type' => 'integer', 'start_at' => 'date_format:Y-m-d H:i:s', 'end_at' => 'date_format:Y-m-d H:i:s']);


        if (request('title')) {
            $query->where('title', 'like', '%' . request('title') . '%');
        }


        if (request('type')) {
            $query->where('type', request('type'));
        }

        if (request('source')) {
            $query->where('source', request('source'));
        }

        if (request('status')) {
            $query->where('status', request('status'));
        }

        if (request('reason_type')) {
            $query->where('reason_type', request('reason_type'));
        }

        if (request('user_name')) {
            $query->where('user_name', request('user_name'));
        }

        if (request('user_mobile')) {
            $query->where('user_mobile', request('user_mobile'));
        }

        if (request('user_ip')) {
            $query->where('user_ip', request('user_ip'));
        }

        if (request('start_at')) {
            $query->where('created_at', '>', request('start_at'));
        }

        if (request('end_at')) {
            $query->where('created_at', '<', request('end_at'));
        }


        return Api::success($query->paginate(request('per_page', 15)));
    }


}
